home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: quick decision: is n a power of 2?
- Date: Sun, 21 Jan 1996 12:12:28 GMT
- Organization: Netcom
- Message-ID: <31022d1e.215811776@nntp.ix.netcom.com>
- References: <4dpd94$c25@fountain.mindlink.net> <4dsaco$a39@newsbf02.news.aol.com> <9601211103.AA19325@dxmint.cern.ch>
- NNTP-Posting-Host: ix-dc10-02.ix.netcom.com
- X-NETCOM-Date: Sun Jan 21 4:12:15 AM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- Dan Pop <danpop@mail.cern.ch> wrote:
-
- > babycox@aol.com (BabyCox) writes:
- >
- > >Here is a revision, this should work (but that's what I said last time)
- >
- > You're a little bit closer this time :-)
- >
- > >Boolean isPowerOfTwo(long x)
- > >{
- > > long tst;
- > > for(tst=1<<31;tst!=0;tst>>=1)
- > > if (x==tst) return true;
- > >
- > > return false;
- > >}
- >
- > I've already commented about the usage of the undefined Boolean, true and
- > false.
- >
- > This new version will work _only_ on some implementations. To make it
- > portable, tst has to be unsigned long (x should also be unsigned long
- > because negative numbers are _never_ powers of two :-)
- >
- > If your C book hasn't taught you that the behaviour of the >> operator is
- > implementation-defined when the first argument is negative, then it's time
- > to read K&R2.
-
- There's also the problem that it will fail if long is more than 32
- bits and x is greater than pow(2, 32).
-
-
- Michael M Rubenstein
-